<template>
  <div class="container">
    <div class="app-container">
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="showDialog=true">添加角色</el-button>
      </div>
      <el-table :data="list">
        <el-table-column prop="name" label="序号" width="100" align="center">
          <template #default="obj">
            {{ obj.$index + 1 + (pageParams.page - 1) * pageParams.pagesize }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="角色" align="center">
          <template #default="obj">
            <div v-if="!obj.row.isEdit">{{ obj.row.name }}</div>
            <el-input v-else v-model="obj.row.editRowData.name" />
          </template>
        </el-table-column>
        <el-table-column prop="state" label="启用" align="center">
          <template #default="obj">
            <div v-if="!obj.row.isEdit">{{ obj.row.state ? '已启用' : '未启用' }}</div>
            <el-switch v-else v-model="obj.row.editRowData.state" :active-value="1" :inactive-value="0" />
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" align="center" width="300">
          <template #default="obj">
            <div v-if="!obj.row.isEdit">{{ obj.row.description }}</div>
            <el-input v-else v-model="obj.row.editRowData.description" type="textarea" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="obj">
            <div v-if="!obj.row.isEdit">
              <el-button size="mini" type="text" @click="onPermData(obj.row.id)">分配权限</el-button>
              <el-button size="mini" type="text" @click="obj.row.isEdit=true">编辑</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                trigger="click"
                @onConfirm="onDel(obj.row.id)"
              >
                <el-button slot="reference" size="mini" type="text" style="margin-left: 10px;">删除</el-button>
              </el-popconfirm>
            </div>
            <div v-else>
              <el-button size="mini" type="primary" @click="onUpdate(obj)">确定</el-button>
              <el-button size="mini" type="text" @click="onCancel(obj)">取消</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="prev,pager,next" :total="pageParams.total" :page-size="pageParams.pagesize" @current-change="onChnagePage" />
    </div>
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog">
      <!-- 表单内容 -->
      <el-form ref="roleForm" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" style="width:300px" size="mini" />
        </el-form-item>

        <el-form-item label="启用" prop="state">
          <el-switch v-model="formData.state" size="mini" :active-value="1" :inactive-value="0" />
        </el-form-item>

        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formData.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="onSumbit">确定</el-button>

              <el-button size="mini" @click="showDialog=false">取消</el-button>

            </el-col>

          </el-row>

        </el-form-item>

      </el-form>

    </el-dialog>

    <!-- 放置权限弹层 -->
    <el-dialog :visible.sync="permShow" title="分配权限">
      <!-- 放置权限数据 -->
      <el-tree
        ref="treeRef"
        :data="permList"
        :props="{ label: 'name' }"
        default-expand-all
        :expand-on-click-node="false"
        show-checkbox
        :default-checked-keys="permId"
        node-key="id"
        check-strictly
      />
      <el-row type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" size="mini" @click="onPerm">确定</el-button>

          <el-button size="mini" @click="permShow=false">取消</el-button>

        </el-col>
      </el-row></el-dialog>

  </div>
</template>
<script>
import { getPermissionListAPI } from '@/api/permission'
import { addRoleAPI, assignPermAPI, deleteRoleAPI, getRoleAPI, getRoleDetailAPI, updateRoleAPI } from '@/api/role'
import { transListToTreeData } from '@/utils'

export default {
  name: 'Role',
  data() {
    return {
      permShow: false,
      permId: [],
      permList: [],
      list: [],
      pageParams: {
        page: 1, // 第几页
        pagesize: 5, // 每页多少条
        total: 0
      },
      showDialog: false,
      formData: {
        name: '',
        description: '',
        state: 0
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    showDialog(newValue) {
      if (!newValue) {
        this.$refs.roleForm.resetFields()
        this.formData = {
          name: '',
          description: '',
          state: 0
        }
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const res = await getRoleAPI(this.pageParams)
      console.log('res  ----->  ', res)
      this.list = res.rows
      this.pageParams.total = res.total
      this.list.forEach((item) => {
        this.$set(item, 'isEdit', false)
        // 复制出来一份数据
        this.$set(item, 'editRowData', {
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
    },
    onChnagePage(page) {
      this.pageParams.page = page
      this.getRoleList()
    },
    async onSumbit() {
      await this.$refs.roleForm.validate()
      await addRoleAPI(this.formData)
      this.getRoleList()
      this.$message.success('添加成功')
      this.showDialog = false
    },
    onCancel(obj) {
      obj.row.editRowData = {
        name: obj.row.name,
        description: obj.row.description,
        state: obj.row.state
      }
      obj.row.isEdit = false
    },
    async onUpdate(obj) {
      if (!obj.row.editRowData.name) {
        this.$message.error('请输入角色名称')
        return
      }
      if (!obj.row.editRowData.description) {
        return this.$message.error('请输入角色描述')
      }
      await updateRoleAPI({
        id: obj.row.id,
        name: obj.row.editRowData.name,
        description: obj.row.editRowData.description,
        state: obj.row.editRowData.state
      })
      this.$message.success('修改成功')
      this.getRoleList()
    },
    async onDel(id) {
      await deleteRoleAPI(id)
      this.$message.success('删除成功')
      this.getRoleList()
    },
    async  onPermData(id) {
      this.permShow = true
      this.permList = transListToTreeData(await getPermissionListAPI(), 0)
      const xxx = await getRoleDetailAPI(id)
      this.permId = xxx.permIds
      this.pid = id
      console.log('xxx  ----->  ', xxx)
    },
    async onPerm() {
      const checkedKeys = this.$refs.treeRef.getCheckedKeys()
      console.log('checkedKeys  ----->  ', checkedKeys)
      await assignPermAPI({ id: this.pid, permIds: checkedKeys })
      this.$message.success('分配成功')
      this.permShow = false
    }
  }
}
</script>
